﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace ReportDesignerServer.Common
{
    /// <summary>
    /// 配置文件读取帮助类
    /// </summary>
    public class AppSettingHelper
    {
        /// <summary>
        /// 注入的配置对象
        /// </summary>
        private static IConfiguration Configuration { get; set; }

        /// <summary>
        /// 设置配置对象
        /// </summary>
        public static void SetConfig(IConfiguration con)
        {
            Configuration = con;
        }

        /// <summary>
        /// 获取层级节点配置 "Cors" -> "IPs"
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string App(params string[] sections)
        {
            try
            {
                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return "";
        }


        /// <summary>
        /// 递归获取配置信息数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static T GetList<T>(string sections)
        {
            var sec = Configuration.GetSection(sections);
            T res = sec.Get<T>();
            return res;
        }

        /// <summary>
        /// 通过节点获取一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetModel<T>(string key) where T : new()
        {
            var sec = Configuration.GetSection(key);
            T res = sec.Get<T>();
            return res;
        }

        /// <summary>
        /// 获取单节点字符串数据
        /// </summary>
        /// <param name="key">eg: WeChat:Token</param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            return Configuration[key];
        }
    }
}